package com.ruoyi.web.controller.card;

import com.ruoyi.card.domain.CardInfo;
import com.ruoyi.card.service.ICardInfoService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 卡信息Controller
 *
 * @author ruoyi
 * @date 2023-06-05
 */
@Controller
@RequestMapping("/card/info")
public class CardInfoController extends BaseController {
    private String prefix = "card/info";

    @Autowired
    private ICardInfoService cardInfoService;

    @RequiresPermissions("card:info:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    /**
     * 查询卡信息列表
     */
    @RequiresPermissions("card:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CardInfo cardInfo) {
        startPage();
        Long userId = ShiroUtils.getUserId();
        if (!SysUser.isAdmin(userId)) {
            cardInfo.setUserId(userId);
        }
        List<CardInfo> list = cardInfoService.selectCardInfoList(cardInfo);
        return getDataTable(list);
    }

    /**
     * 导出卡信息列表
     */
    @RequiresPermissions("card:info:export")
    @Log(title = "卡信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CardInfo cardInfo) {
        Long userId = ShiroUtils.getUserId();
        if (!SysUser.isAdmin(userId)) {
            cardInfo.setUserId(userId);
        }
        List<CardInfo> list = cardInfoService.selectCardInfoList(cardInfo);
        ExcelUtil<CardInfo> util = new ExcelUtil<CardInfo>(CardInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 新增卡信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存卡信息
     */
    @RequiresPermissions("card:info:add")
    @Log(title = "卡信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CardInfo cardInfo) {
        Long userId = ShiroUtils.getUserId();
        if (userId != null) {
            cardInfo.setUserId(userId);
        }
        return toAjax(cardInfoService.insertCardInfo(cardInfo));
    }

    /**
     * 修改卡信息
     */
    @GetMapping("/edit/{cardId}")
    public String edit(@PathVariable("cardId") Long cardId, ModelMap mmap) {
        CardInfo cardInfo = cardInfoService.selectCardInfoById(cardId);
        mmap.put("cardInfo", cardInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存卡信息
     */
    @RequiresPermissions("card:info:edit")
    @Log(title = "卡信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CardInfo cardInfo) {
        Long userId = ShiroUtils.getUserId();
        if (!SysUser.isAdmin(userId)) {
            cardInfo.setUserId(userId);
        }
        return toAjax(cardInfoService.updateCardInfo(cardInfo));
    }

    /**
     * 删除卡信息
     */
    @RequiresPermissions("card:info:remove")
    @Log(title = "卡信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(cardInfoService.deleteCardInfoByIds(ids));
    }
}
